package TiAnGo;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author czl
 * @Date 2024-03-22
 */
public class Box {
    private LinkedList<Fruit> boxList = new LinkedList<>();

    private static final int BOX_MAX = 5;


    public synchronized void put(String name , Fruit fruit){
        while (boxList.size() == BOX_MAX) {
            try {

                this.notifyAll();

                this.wait();


            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        boxList.add(fruit);

        System.out.println("篮子当前容量：" + boxList.size() + "，男孩子[" + name + "]摘水果:" + fruit);

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void take(String name) {

        while (boxList.size() == 0) {
            try {
                this.notifyAll();

                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        Fruit fruit = this.boxList.removeLast();

        System.out.println("篮子当前容量：" + boxList.size() + "，女孩子[" + name + "]吃水果:" + fruit);

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
